/**
 * Created by tanbingqin on 2019/1/26.
 */
import {messages} from "share/common";
/**
 * 操作：关闭
 * 适用：差旅，费用申请单
 */
import React from 'react'
import { connect } from 'react-redux'
import menuRoute from 'routes/menuRoute'
import { Button, Modal, Select, message, Row, Col, Alert } from 'antd'
import requestService from 'containers/request/request.service'

class CloseBtn extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      loading: false,
      applicationCloseList: menuRoute.getRouteItem('request-close','key'), //申请单关闭列表页
    }
  }

  componentWillMount(){}

  componentWillReceiveProps(nextProps) {}

  handleClose = () => {
    let params = {
      applicationOID: this.props.applicationOID,
      closeAllApportionment: false, //是否整单关闭
      apportionmentOIDs: this.props.apportionmentOIDs,
      expenseBudgetOIDs: this.props.budgetOIDs
    };
    this.setState({loading: true});
    requestService.setCloseExpense(params).then(res => {
      this.setState({loading: false});
      message.success(messages('request.key548')/*关闭成功*/);
      this.context.router.push(this.state.applicationCloseList.url);
    }).catch(err =>{
      if (err.response && err.response.data) {
        message.error(err.response.data.message);
      }
      this.setState({loading: false});
    });
  };

  render() {
    const {loading} = this.state;
    return (
      <div className="request-btn">
        <Button type="primary"
                loading={loading}
                disabled={!this.props.apportionmentOIDs.length && !this.props.budgetOIDs.length}
                onClick={this.handleClose}>
          {messages('request.key102')/*关闭*/}
        </Button>
      </div>
    )
  }
}

CloseBtn.propTypes = {
  apportionmentOIDs: React.PropTypes.array,
  budgetOIDs: React.PropTypes.array,
  applicationOID: React.PropTypes.string
};

CloseBtn.defaultProps = {
  apportionmentOIDs: [],
  budgetOIDs: [],
  applicationOID: ''
};

CloseBtn.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    loginCompany: state.login.loginCompany,
    loginUser: state.login.loginUser
  }
}

export default connect(mapStateToProps)(CloseBtn)
